Python PEP249: track connections stored in class instance attributes#21889
Draft
Copilot wants to merge 2 commits into
Draft
Python PEP249: track connections stored in class instance attributes#21889Copilot wants to merge 2 commits into
Copilot wants to merge 2 commits into
Conversation
Introduce two new Connection::InstanceSource subclasses in PEP249.qll: - ConnectionGetterAttributeRead: recognises self._conn reads inside getter methods of classes whose __init__ stores a connect() call in that attribute. The AttrRead node coincides with the return node, so the existing TypeTracker returnStep propagates the connection type to all call sites automatically. - ConnectionConstructorAttributeRead: recognises ClassName()._conn direct attribute reads on constructor-call results. Both classes share the classStoresConnectionInInit helper predicate that checks for the self.attr = dbapi.connect() store pattern in __init__. Also adds test cases for the new patterns in the hdbcli test suite and a change note.
Copilot
AI
changed the title
Fix PEP249 connection tracking through class attribute wrappers
Python PEP249: track connections stored in class instance attributes
May 21, 2026
Copilot created this pull request from a session on behalf of
owen-mc
May 21, 2026 23:25
View session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SQL injection (and other PEP 249-based) queries miss findings when a
connect()result is stored in a class attribute (self._conn) and later accessed via a getter method or direct attribute read — the TypeTracker cannot follow the value through__init__becauseReturnStepis blocked after aCallStep.Changes
python/ql/lib/semmle/python/frameworks/PEP249.qllclassStoresConnectionInInit(cls, attrName)— detectsself.<attr> = dbapi.connect(...)in__init__ConnectionGetterAttributeRead(InstanceSource) — marksself._connreads inside non-__init__methods as connection sources. TheAttrReadnode forreturn self._connis simultaneously the function'sExtractedReturnNode, so the existingreturnSteppropagates the type to all call sites for freeConnectionConstructorAttributeRead(InstanceSource) — marksMyClass()._conndirect attribute reads as connection sources, usingDataFlowDispatch::resolveClassCallto confirm the constructor targets the right classpython/ql/test/library-tests/frameworks/hdbcli/pep249.py— test cases for the new patternspython/ql/src/change-notes/2026-05-21-pep249-class-wrapper-connections.md— change notePatterns now covered